package com.back.controller;

import com.back.annotation.AuthCheck;
import com.back.common.BaseResponse;
import com.back.common.DeleteRequest;
import com.back.common.ResultUtils;
import com.back.constant.UserConstant;
import com.back.exception.BusinessException;
import com.back.exception.ErrorCode;
import com.back.exception.ThrowUtils;
import com.back.model.dto.skill.SkillAddRequest;
import com.back.model.dto.skill.SkillQueryRequest;
import com.back.model.dto.skill.SkillUpdateRequest;
import com.back.model.vo.skill.SkillVO;
import com.back.service.SkillService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 技能接口
 */
@RestController
@RequestMapping("/skill")
@Slf4j
public class SkillController {

    @Resource
    private SkillService skillService;

    // region 增删改查

    /**
     * 创建技能
     *
     * @param skillAddRequest 技能添加请求
     * @return 技能ID
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addSkill(@RequestBody SkillAddRequest skillAddRequest) {
        ThrowUtils.throwIf(skillAddRequest == null, ErrorCode.PARAMS_ERROR);
        
        long skillId = skillService.addSkill(skillAddRequest);
        return ResultUtils.success(skillId);
    }

    /**
     * 删除技能
     *
     * @param deleteRequest 删除请求
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteSkill(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        long id = deleteRequest.getId();
        boolean result = skillService.deleteSkill(id);
        return ResultUtils.success(result);
    }

    /**
     * 更新技能（仅管理员）
     *
     * @param skillUpdateRequest 技能更新请求
     * @return 是否更新成功
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateSkill(@RequestBody SkillUpdateRequest skillUpdateRequest) {
        if (skillUpdateRequest == null || skillUpdateRequest.getSkillId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        boolean result = skillService.updateSkill(skillUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取技能（封装类）
     *
     * @param id 技能ID
     * @return 技能VO
     */
    @GetMapping("/get/vo")
    public BaseResponse<SkillVO> getSkillVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        
        SkillVO skillVO = skillService.getSkillVOById(id);
        return ResultUtils.success(skillVO);
    }

    /**
     * 分页获取技能列表（封装类）
     *
     * @param skillQueryRequest 技能查询请求
     * @return 技能VO分页列表
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<IPage<SkillVO>> listSkillVOByPage(@RequestBody SkillQueryRequest skillQueryRequest) {
        int current = skillQueryRequest.getCurrent();
        int size = skillQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        
        IPage<SkillVO> skillVOPage = skillService.listSkillVOByPage(skillQueryRequest);
        return ResultUtils.success(skillVOPage);
    }

    /**
     * 根据英雄ID获取技能列表
     *
     * @param heroId 英雄ID
     * @return 技能VO列表
     */
    @GetMapping("/list/hero/{heroId}")
    public BaseResponse<List<SkillVO>> getSkillListByHeroId(@PathVariable Integer heroId) {
        ThrowUtils.throwIf(heroId == null || heroId <= 0, ErrorCode.PARAMS_ERROR);
        
        List<SkillVO> skillVOList = skillService.getSkillListByHeroId(heroId);
        return ResultUtils.success(skillVOList);
    }

    // endregion
}